//
//  ShenPiViewController.m
//  JuBoCRM
//
//  Created by iMac on 2024/1/24.
//  Copyright © 2024 iningKe. All rights reserved.
//

#import "ShenPiViewController.h"
#import "FaQiShenPiViewController.h"
#import "ShenPiJiLuViewController.h"
#import "ShenPiCollectionViewCell.h"
@interface ShenPiViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
    NSArray *dataSource;
}
@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingScrollView *mainScrollView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet UICollectionView *mainCollectionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mainCollectionViewHeight;

@end

@implementation ShenPiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.customNavBar.title = @"审批";
    if (@available(iOS 11.0, *)) {
        self.mainScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    self.topSpace.constant = NavaBarHeight;
    
    [_mainCollectionView registerNib:[UINib nibWithNibName:@"ShenPiCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ShenPiCollectionViewCell"];
    
    [self getInfoFromServer];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getInfoFromServer];
    }];

    _mainScrollView.mj_header = header;
}


- (void)getInfoFromServer{
    NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
    [paraDic setValue:@"0" forKey:@"pageType"];
    [paraDic setValue:@"0" forKey:@"label"];
    [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/examines/queryPartList" andParas:paraDic andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            NSLog(@"info:%@",info);
            dataSource = [[info objectForKey:@"data"] objectForKey:@"list"];
            [_mainCollectionView reloadData];
            
            NSInteger row = dataSource.count/3;
            if(dataSource.count % 3 != 0){
                row++;
            }
            _mainCollectionViewHeight.constant = 85*row;
        }
        [_mainScrollView.mj_header endRefreshing];
    }];
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return dataSource.count;
}
//创建cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShenPiCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShenPiCollectionViewCell" forIndexPath:indexPath];
    NSDictionary *dataDic = dataSource[indexPath.row];
    cell.itemName.text = GetHandleString(dataDic, @"examineName");
    return cell;
}

//设置item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((ScreenWidth-32-40)/3, 85);
}
//设置垂直间距,默认的垂直和水平间距都是10
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
//设置水平间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

//四周的边距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 20, 0, 20);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    FaQiShenPiViewController *faQi = [[FaQiShenPiViewController alloc] init];
    NSDictionary *dataDic = dataSource[indexPath.row];
    faQi.examineId = GetHandleString(dataDic, @"examineId");
    faQi.label = GetHandleString(dataDic, @"label");
    [self pushViewController:faQi];
}
- (IBAction)tapClick:(UITapGestureRecognizer *)sender {
    ShenPiJiLuViewController *faQi = [[ShenPiJiLuViewController alloc] init];
    faQi.currentIndex = sender.view.tag-101;
    [self pushViewController:faQi];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
